package org.makcro.sms.services.chat.web.client.widgets;

import org.makcro.sms.services.chat.web.client.AppEvents;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;

public class AnswerTextPanel extends ContentPanel {

	private Text lblAnswerStats;
	private TextArea txtAnswerText;

	public AnswerTextPanel() {
		setHeading("Tekst odgovora");
		setLayout(new RowLayout(Orientation.VERTICAL));
		add(getLblAnswerStats(), new RowData(Style.DEFAULT, Style.DEFAULT, new Margins(5, 5, 5, 5)));
		add(getTxtAnswerText(), new RowData(.98, 1.0, new Margins(0, 5, 5, 5)));
	}

	public Text getLblAnswerStats() {
		if (lblAnswerStats == null) {
			lblAnswerStats = new Text("");
		}
		return lblAnswerStats;
	}

	public TextArea getTxtAnswerText() {
		if (txtAnswerText == null) {
			txtAnswerText = new TextArea();
			txtAnswerText.addKeyListener(new KeyListener() {
				public void componentKeyUp(ComponentEvent event) {
					onAnswerTextModified();
				}
			});
			txtAnswerText.setFieldLabel("New TextArea");
			txtAnswerText.setMinLength( 5 );
			txtAnswerText.setAutoValidate( true );
			txtAnswerText.getMessages().setMinLengthText( "Odgovor mora sadrzati najmanje 5 slova" );
			
			
		}
		return txtAnswerText;
	}

	protected void onAnswerTextModified() {
		Dispatcher.get().dispatch(AppEvents.ANSWER_TEXT_CHANGED);
	}

	public void reset() {
		getTxtAnswerText().setValue( "" );
		getLblAnswerStats().setText( "" );
		
	}
}
